package xp2p.trashes;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class BytesConverter
{
	public static byte[] objToBytes(Serializable object) throws IOException
	{
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutput out = new ObjectOutputStream(bos);   
		out.writeObject(object);
		byte[] yourBytes = bos.toByteArray();
		out.close();
		bos.close();
		return yourBytes;		
	}
	
	public static Serializable bytesToObj(byte[] bytes) throws IOException, ClassNotFoundException
	{
		ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
		ObjectInput in = new ObjectInputStream(bis);
		Serializable o = (Serializable) in.readObject();
		bis.close();
		in.close();
		return o;
	}
}
